/*
 * @Description ：
 * @Author：阳寜
 * @Date：2024/8/17 10:00
 * @LastModified：2024/8/17 10:09
 */
package blogcatback.utils;

import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class MD5Utils {
    /**
     * @Param:str 要加密的字符串
     * @return    加密后的字符串
     * */
    public static String Code(String str) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(str.getBytes());
            byte[] byteDigest = messageDigest.digest();
            int i;
            StringBuilder builder = new StringBuilder();
            for (byte b : byteDigest) {
                i = b;
                if (i < 0)
                    i += 256;
                if (i < 16)
                    builder.append("0");
                builder.append(Integer.toHexString(i));
            }
            //32位加密
            return builder.toString();
            //16位的加密
            //return buffer.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage());
            return null;
        }
    }
}
